package com.zjsru.leetcode75.level1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: cookLee
 * @Date: 2024-01-02
 * 搜索推荐系统
 */
public class SuggestedProducts {

    /**
     * 主
     * \
     * 输入：products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
     * 输出：[
     * ["mobile","moneypot","monitor"],
     * ["mobile","moneypot","monitor"],
     * ["mouse","mousepad"],
     * ["mouse","mousepad"],
     * ["mouse","mousepad"]
     * ]
     * 解释：按字典序排序后的产品列表是 ["mobile","moneypot","monitor","mouse","mousepad"]
     * 输入 m 和 mo，由于所有产品的前缀都相同，所以系统返回字典序最小的三个产品 ["mobile","moneypot","monitor"]
     * 输入 mou， mous 和 mouse 后系统都返回 ["mouse","mousepad"]
     * \
     * 输入：products = ["havana"], searchWord = "havana"
     * 输出：[["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
     * \
     * 输入：products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
     * 输出：[["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
     * \
     * @param args args
     */
    public static void main(String[] args) {
        String[] products = new String[]{"mobile","mouse","moneypot","monitor","mousepad"};
        String searchWord = "mouse";
        SuggestedProducts suggestedProducts = new SuggestedProducts();
        System.out.println(suggestedProducts.suggestedProducts(products, searchWord));
    }

    /**
     * 建议产品 ：按照前缀寻找
     *
     * @param products   产品
     * @param searchWord 搜索词
     * @return {@link List}<{@link List}<{@link String}>>
     */
    public List<List<String>> suggestedProducts(String[] products, String searchWord) {
        Arrays.sort(products);
        List<List<String>> list = new ArrayList<>();
        for (int i = 1; i <= searchWord.length(); i++) {
            //获取前缀
            String pre = searchWord.substring(0, i);
            List<String> searchProduct = new ArrayList<>();
            for (String product : products) {
                if (product.startsWith(pre)) {
                    searchProduct.add(product);
                }
                //限制数量不超过3个
                if (searchProduct.size() >= 3) {
                    break;
                }
            }
            list.add(searchProduct);
        }
        return list;
    }

}
